package com.wlc.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.wlc.pojo.Person;
import com.wlc.service.PersonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 消费者
 * @author milicool
 * Created on 2018/9/14
 */
@Component
public class Comsumer {
    @Resource
    private PersonService personService;
    private Logger log = LoggerFactory.getLogger(Comsumer.class);

    @RabbitListener(queues = "queue-test")
    public void process(Message message, Channel channel) throws IOException {
       /* channel.exchangeDeclare("queue-test", BuiltinExchangeType.FANOUT);*/
        // 采用手动应答模式, 手动确认应答更为安全稳定
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        //log.info("receive: " + new String(message.getBody()));
        Person person = new Person();
        person.setName(new String(message.getBody()));
        personService.save(person);
        System.out.println("第一个保存成功");
    }
}